Best Time to Buy and Sell Stock 发表于 2019-03-14 | 分类于 贪心 , leetcode https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 123456789101112131415class Solution {public: int maxProfit(vector<int>& prices) { int n=prices.size(); if(n<2) return 0;//?为什么在循环调不出来 int cur_min=prices[0];//定义当前最小价格 int max_profit=0; for(int i=1;i<n;i++) { max_profit=max(max_profit,prices[i]-cur_min); cur_min=min(cur_min,prices[i]); } return max_profit; }};